<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经度" prop="longitude">
        <el-input
          v-model="queryParams.longitude"
          placeholder="请输入经度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="纬度" prop="latitude">
        <el-input
          v-model="queryParams.latitude"
          placeholder="请输入纬度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input
          v-model="queryParams.sort"
          placeholder="请输入排序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否开放" prop="isOpen">
        <el-select v-model="queryParams.isOpen" placeholder="请选择是否开放" clearable>
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="开放次数" prop="openTime">
        <el-input
          v-model="queryParams.openTime"
          placeholder="请输入开放次数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:venue:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:venue:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:venue:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:venue:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="venueList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="经度" align="center" prop="longitude" />
      <el-table-column label="纬度" align="center" prop="latitude" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="是否开放" align="center" prop="isOpen">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.isOpen"/>
        </template>
      </el-table-column>
      <el-table-column label="开放次数" align="center" prop="openTime" />
<!--      <el-table-column label="详情" align="center" prop="info" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:venue:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:venue:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改场馆对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" id="fromHeight">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="经纬度" prop="longitude">
          <span v-if="form.longitude&&form.latitude">{{ `${form.longitude},${form.latitude}` }}</span>
          <el-button @click="mapVisible=true" size="mini">选择地址</el-button>
        </el-form-item>
<!--        <el-form-item label="经度" prop="longitude">-->
<!--          <el-input v-model="form.longitude" placeholder="请输入经度" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="纬度" prop="latitude">-->
<!--          <el-input v-model="form.latitude" placeholder="请输入纬度" />-->
<!--        </el-form-item>-->
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="是否开放" prop="isOpen">
          <el-select v-model="form.isOpen" placeholder="请选择是否开放">
            <el-option
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开放次数" prop="openTime">
          <el-input v-model="form.openTime" placeholder="请输入开放次数" />
        </el-form-item>
        <el-form-item label="详情">
          <editor v-model="form.info" :min-height="192"/>
        </el-form-item>
        <el-divider content-position="center">开放时间信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddProcuOpenTime">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteProcuOpenTime">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="procuOpenTimeList" :row-class-name="rowProcuOpenTimeIndex" @selection-change="handleProcuOpenTimeSelectionChange" ref="procuOpenTime">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="开放时间表" prop="openTime" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.openTime" placeholder="请输入开放时间表" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <mapIndex :visible.sync="mapVisible" :formData="form" @editFrom="editFrom"></mapIndex>
  </div>
</template>

<script>
import { listVenue, getVenue, delVenue, addVenue, updateVenue } from "@/api/system/venue";
import mapIndex from "@/components/Map/index.vue";
export default {
  name: "Venue",
  dicts: ['sys_normal_disable'],
  components: {mapIndex},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedProcuOpenTime: [],
      mapVisible:false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 场馆表格数据
      venueList: [],
      // 开放时间表格数据
      procuOpenTimeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        longitude: null,
        latitude: null,
        sort: null,
        isOpen: null,
        openTime: null,
        info: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        longitude: [
          { required: true, message: "经度不能为空", trigger: "blur" }
        ],
        latitude: [
          { required: true, message: "纬度不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询场馆列表 */
    getList() {
      this.loading = true;
      listVenue(this.queryParams).then(response => {
        this.venueList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        longitude: null,
        latitude: null,
        sort: null,
        isOpen: null,
        openTime: null,
        info: null
      };
      this.procuOpenTimeList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    editFrom(option) {
      this.form.longitude = option.map.longitude
      this.form.latitude = option.map.latitude
      this.mapVisible = false
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加场馆";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getVenue(id).then(response => {
        this.form = response.data;
        this.procuOpenTimeList = response.data.procuOpenTimeList;
        this.open = true;
        this.title = "修改场馆";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.procuOpenTimeList = this.procuOpenTimeList;
          if (this.form.id != null) {
            updateVenue(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVenue(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除场馆编号为"' + ids + '"的数据项？').then(function() {
        return delVenue(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 开放时间序号 */
    rowProcuOpenTimeIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 开放时间添加按钮操作 */
    handleAddProcuOpenTime() {
      let obj = {};
      obj.openTime = "";
      this.procuOpenTimeList.push(obj);
    },
    /** 开放时间删除按钮操作 */
    handleDeleteProcuOpenTime() {
      if (this.checkedProcuOpenTime.length == 0) {
        this.$modal.msgError("请先选择要删除的开放时间数据");
      } else {
        const procuOpenTimeList = this.procuOpenTimeList;
        const checkedProcuOpenTime = this.checkedProcuOpenTime;
        this.procuOpenTimeList = procuOpenTimeList.filter(function(item) {
          return checkedProcuOpenTime.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleProcuOpenTimeSelectionChange(selection) {
      this.checkedProcuOpenTime = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/venue/export', {
        ...this.queryParams
      }, `venue_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
